package cn.com.surker.system.controller;

import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.entity.DataSourceInfo;
import cn.com.surker.system.service.DataSourceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* <p>
    * 数据源配置表 前端控制器
    * </p>
*
* @author lucky
* @since 2021-04-26
*/

@RestController
@RequestMapping("/console/data/source")
public class DataSourceController {

    /**实业实现**/
    @Resource
    private DataSourceService serviceImpl;

    /**
     *  分页查询列表
     * @param dto
     * @param pageIndex 每页开始数
     * @param limit 每页大小
     **/
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RestResultDto findAll(DataSourceInfo dto, Integer pageIndex, Integer limit){
        try {
            Page<DataSourceInfo> page = new Page<>(pageIndex, limit);
            IPage iPage = serviceImpl.findAllByPage(dto, (pageIndex - 1) * limit, limit);
            return RestResultDto.success(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,数据加载失败");
        }
    }

    /**
     *  新增数据
     * @param dto
     **/
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    @ResponseBody
    public RestResultDto save(DataSourceInfo dto){
        try {
            return RestResultDto.complete(serviceImpl.saveInit(dto));
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,添加数据失败");
        }
    }

    /**
     * 更新数据
     * @param dto
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public RestResultDto update(@PathVariable Long id, DataSourceInfo dto) {
        try {
            return RestResultDto.complete(serviceImpl.update(dto));
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,更新数据失败");
        }
    }

    /**
     * 更新数据
     * @param status 状态(0:启用，1:禁用)
     * @param id 标识
     * @return
     */
    @RequestMapping(value = "/update/status/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public RestResultDto updateStatus(@PathVariable Long id, Integer status) {
        try {
            return RestResultDto.complete(serviceImpl.updateStatus(id, status));
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,更新数据失败");
        }
    }

}



